<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/3/13
 * Time: 18:21
 */

namespace app\api\behavior;
use app\common\model\GameDota2;
use app\common\model\GameLol;
use app\common\model\GamePvp;
use app\common\model\User;
use think\Cache;
use app\common\model\Bets;
use app\common\model\BetsRecord;
use app\common\model\Config;
use app\api\Service\GameService;
use app\api\library\SecretSys;
use think\Env;

class UsersBetedAfter
{
    public $num = ['double','single'];
    public $sex = ['man','woman'];
    public $type = ['close','far'];
    public $magic = ['magic_y','magic_n'];
    public $line = ['tianhui','yemo'];
    public $attr = ['power','agile','intellect'];
    public $magic_y = 1;
    public $magicArr = ['magic_y'=>1,'magic_n'=>0];

    public function run(&$params)
    {

        $condition = [
            'game'  => $params['game'],
            'index' => $params['index']
        ];
        $betsByIndex = collection(BetsRecord::where($condition)->select())->toArray();

        $bets = [];
        foreach ($betsByIndex as $bet){
            $bets[$params['index']][$bet['type']] = $bet['money'];
        }

        //单双 lol，pvp，dota2共同包含
        $double = $this->getExpectIncome('double',$bets,$params);
        $single = $this->getExpectIncome('single',$bets,$params);
        if( $single > $double ){
            $knum = 'double';
        }elseif($single == $double){
            $knum = $this->num[mt_rand(0,1)];
        }else{
            $knum = 'single';
        }
        //攻击方式 lol，pvp，dota2共同包含
        $close = $this->getExpectIncome('close',$bets,$params);
        $far = $this->getExpectIncome('far',$bets,$params);
        if( $close > $far ){
            $ktype = 'far';
        }elseif( $close == $far ){
            $ktype = $this->type[mt_rand(0,1)];
        }else{
            $ktype = 'close';
        }

        if( $params['game'] == 'lol' || $params['game'] == 'pvp' ){
            //性别
            $man = $this->getExpectIncome('man',$bets,$params);
            $woman = $this->getExpectIncome('woman',$bets,$params);
            if( $man > $woman ){
                $ksex = 'woman';
            }elseif($man == $woman){
                $ksex = $this->sex[mt_rand(0,1)];
            }else{
                $ksex = 'man';
            }

            //有无魔法值
            $magic_y = $this->getExpectIncome('magic_y',$bets,$params);
            $magic_n = $this->getExpectIncome('magic_n',$bets,$params);
            if( $magic_y > $magic_n ){
                $kmagic = 'magic_n';
            }elseif($magic_y == $magic_n){
                $kmagic = $this->magic[mt_rand(0,1)];
            }else{
                $kmagic = 'magic_y';
            }

            $kaijiang = [
                'num'   => $knum,
                'sex'   => $ksex,
                'magic' => $kmagic,
                'type'  => $ktype
            ];
            return $this->matchingLotteryRes($params['game'],$kaijiang);
        }elseif( $params['game'] == 'dota2' ){
            //属性
            $power = $this->getExpectIncome('power',$bets,$params);
            $agile = $this->getExpectIncome('agile',$bets,$params);
            $intellect = $this->getExpectIncome('intellect',$bets,$params);
            $attr = ['power'=>$power,'agile'=>$agile,'intellect'=>$intellect];
            $kattr = getMinByArr($attr);

            //阵营
            $tianhui = $this->getExpectIncome('tianhui',$bets,$params);
            $yemo    = $this->getExpectIncome('yemo',$bets,$params);
            $line    = ['tianhui'=>$tianhui,'yemo'=>$yemo];
            $kline   = getMinByArr($line);
            $kaijiang = [
                'num'    => $knum,
                'line'   => $kline,
                'attr'   => $kattr,
                'type'   => $ktype
            ];
            return $this->matchingLotteryRes($params['game'],$kaijiang);

        }
    }

    //获取预计收入
    public function getExpectIncome($type,$bets,$params)
    {
        $GameService = new  GameService();
        $config = $GameService->getGameOdds($params['game'].'odds');
        $odds  = (float)$config[$type];
        $money = isset($bets[$params['index']][$type]) ? (int)$bets[$params['index']][$type] : 0;
        return $odds * $money;

    }

    /**
     * $data 计算的开奖结果
     * 匹配开奖结果,根据投注量，计算开奖结果
     */
    public function matchingLotteryRes($game,$data)
    {
        //$SYSACOUT = SecretSys::SECRET_SYS_ACOUT;

        $SYSACOUT = Config::where('group','blacklist')->where('name','username')->value('value');
        if( empty($SYSACOUT) ){
            $SYSACOUT = 'a12389b';
        }

        $sysuserid = User::where('username',$SYSACOUT)->value('id');
        $sysbet = Bets::where('user_id',$sysuserid)->where('game',$game)->where('status',NULL)->select();

        if( $sysbet ){
            foreach ($sysbet as $k2=>$v2){
                $sysbetcontent = json_decode($v2['content'],true);
                foreach ($sysbetcontent as $ks=>$vs){
                    if( $ks == 'single'){
                        $data['num'] = 'double';
                        $sysk = 'double';
                    }elseif($ks == 'double'){
                        $data['num'] = 'single';
                        $sysk = 'single';
                    }
                }
            }

//            foreach ($sysbetcontent as $ks=>$vs){
//                //if( $vs >= SecretSys::SECRET_SYS_AM ){
//                    $sysk = $ks;
//                    $sysbm = $vs;
//                //}
//            }
//            if( isset($sysbm) && $sysbm ){ //......
//                if( $sysk == 'single' || $sysk == 'double' ){
//                    $data['num'] = $sysk;
//                }
//            }

        }
        $sysRes = [];
        $lotteryRes = [];//开奖结果，根据$data匹配出的结果
        $identifier = [];
        if( $game == 'lol' || $game == 'pvp'){//lol 或 pvp
            if( $game == 'lol' ){
                $res = collection(GameLol::cache('gamelol1')->select())->toArray();
            }else{
                $res = collection(GamePvp::cache('gamepvp')->select())->toArray();
            }
            foreach ( $res as $v ){
                if( $v['num'] == $data['num'] && $v['sex'] == $data['sex']
                    && $v['type'] == $data['type'] && $v['magic'] == $this->magicArr[$data['magic']]
                ){
                    $lotteryRes[] = $v;
                    //break; //开启平衡之后请解封，待测试
                }
                if( isset($sysk) && $sysk ){ //......

                    if( $v['num'] == $sysk ){
                        $sysRes[] = $v;
                    }
                }
            }

        }elseif( $game == 'dota2' ){  //dota2
            $res = collection(GameDota2::cache('gamedota2')->select())->toArray();
            foreach ( $res as $v ){
                if( $v['num'] == $data['num'] && $v['attr'] == $data['attr']
                    && $v['type'] == $data['type'] && $v['line'] == $data['line']
                ){
                    $lotteryRes[] = $v;
                    //continue; 开启平衡之后请解封,待测试
                }
                if( isset($sysk) && $sysk ){ //......

                    if( $v['num'] == $sysk ){
                        $sysRes[] = $v;
                    }
                }
            }

        }
        foreach ($res as $v1){
            $identifier[] = $v1['identifier'];
        }
        $balance = Config::where('group','basic')->where('name','balance')->value('value');
        
        if( isset($sysk) && $sysk && !$balance){
            $key = array_rand($sysRes,1);
            return $sysRes[$key]['identifier'];
        }elseif( $balance ){
            if( $lotteryRes ){
                $key = array_rand($lotteryRes,1);
                return $lotteryRes[$key]['identifier'];
            }else{
                return $identifier[array_rand($identifier,1)];
            }
        }else{
            return $identifier[array_rand($identifier,1)];
        }

    }
}